package pl.bristleback.server.bristle.mocks.actions;

import org.apache.log4j.Logger;
import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.token.Token;
import org.springframework.stereotype.Component;
import pl.bristleback.server.bristle.actions.AnnotatedRemoteAction;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.binding.Bind;
import pl.bristleback.server.bristle.binding.ObjectBinder;
import pl.bristleback.server.bristle.binding.Property;
import pl.bristleback.server.bristle.token.Tokenize;
import pl.bristleback.server.bristle.token.TokenizeSet;
import pl.bristleback.server.bristle.token.Tokenizer;

/**
 * Action class mock with required rights.
 * <p/>
 * Created on: 2010-09-03 16:21:32 <br/>
 *
 * @author Wojciech Niemiec
 */
@AnnotatedRemoteAction(actionName = RemoteActionMock.ACTION_NAME, requiredRights = {RemoteActionMock.ACTION_RIGHTS})
@Component
public class RemoteActionMock implements RemoteAction {
  private static Logger log = Logger.getLogger(RemoteActionMock.class.getName());

  public static final String ACTION_NAME = "CONNECTION_STARTED_ACTION_NAME";

  public static final String ACTION_RIGHTS = "admin";

  public static final String ADDED_VARIABLE_NAME = "mockVariable";
  public static final String PERFORM_COUNT_NAME = "performCount";
  public static final String ADDED_VARIABLE_VALUE = "mockVariableValue";

  public static final String TOKEN_HELPER_FIELD = "tokenizer";
  public static final String OBJECT_BINDER_FIELD = "objectBinder";

  public static final String NAMED_TO_TOKEN_NAME = "namedToken";
  public static final String BINDER_ROOT_NAME = "mockBeanRoot";

  @TokenizeSet({
    @Tokenize(parameters = ADDED_VARIABLE_NAME),
    @Tokenize(parameters = {"parameter0", "parameter1"}),
    @Tokenize(parameters = {"parameter0", "parameter1"}, name = NAMED_TO_TOKEN_NAME)
  })
  private Tokenizer tokenizer;

  @Bind(properties = {
    @Property(name = "property1"),
    @Property(name = "property2", required = true),
    @Property(name = "list1", required = true),
    @Property(name = "map1.property1"),
    @Property(name = "map1.property2", required = true),
    @Property(name = "array1.property2"),
    @Property(name = "array2"),
    @Property(name = "nestedBean.property1"),
    @Property(name = "nestedBean.property2", required = true)
  })
  private ObjectBinder<MockBean> objectBinder;

  @Bind(rootName = "mockBeanRoot", properties = {
    @Property(name = "property1"),
    @Property(name = "property2", required = true),
    @Property(name = "list1", required = true),
    @Property(name = "map1.property1", required = true),
    @Property(name = "array1.property2")
  })
  private ObjectBinder<MockBean> objectBinder2;

  private int performCount = 0;

  @Override
  public void performAction(WebSocketConnector connector, Token token) {
    token.setString(ADDED_VARIABLE_NAME, ADDED_VARIABLE_VALUE);
    performCount++;
    token.setInteger(PERFORM_COUNT_NAME, performCount);
  }

  public Tokenizer getTokenizer() {
    return tokenizer;
  }
}
